package com.atguigu.gulimail.product.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.atguigu.gulimail.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimail.product.dao.AttrDao;
import com.atguigu.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.CategoryService;
import com.atguigu.gulimail.product.vo.AttrgroupWithAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.AttrGroupDao;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao,
        AttrGroupEntity> implements AttrGroupService {

    @Autowired
    CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationDao attrgroupRelationDao;
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        final IPage<AttrGroupEntity> page1 = new Query<AttrGroupEntity>().getPage(params);
        final QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            wrapper.and(w -> w.eq("attr_group_id", key).
                    or().like("attr_group_name", key));
        }
        if (null != catelogId && catelogId > 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        IPage<AttrGroupEntity> page = this.page(
                page1,
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public Long[] getCatelogPath(AttrGroupEntity attrGroup) {
        final List<Long> paths = new ArrayList<>();
        Long categoryId = attrGroup.getCatelogId();
        while (categoryId != null && categoryId > 0) {
            paths.add(categoryId);
            final CategoryEntity byId = categoryService.getById(categoryId);
            categoryId = byId.getParentCid();
        }
        Collections.reverse(paths);
        return paths.toArray(new Long[0]);
    }

    @Override
    public List<AttrgroupWithAttrVo> getAttrGroupsWithAttrsByCatelogId(Long catelogId) {

        final List<AttrGroupEntity> attrGroupEntities =
                attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",
                        catelogId));

        List<AttrgroupWithAttrVo> attrgroupsWithAttrVos = new ArrayList<>();
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            final Long[] catelogPath = this.getCatelogPath(attrGroupEntity);
            attrGroupEntity.setCatelogPath(catelogPath);
            final AttrgroupWithAttrVo attrgroupWithAttrVo = new AttrgroupWithAttrVo();
            BeanUtils.copyProperties(attrGroupEntity, attrgroupWithAttrVo);
            final List<AttrAttrgroupRelationEntity> relationEntities =
                    attrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq("attr_group_id", attrGroupEntity.getAttrGroupId()));
            final List<Long> attrIds =
                    relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            if (attrIds.size() > 0) {
                final List<AttrEntity> attrs =
                        this.attrDao.selectBatchIds(attrIds);
                attrgroupWithAttrVo.setAttrs(attrs);
            }
            attrgroupsWithAttrVos.add(attrgroupWithAttrVo);
        }
        return attrgroupsWithAttrVos;
    }
}